﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FamaWebSite.Utils;

namespace FamaWebSite.Controllers
{
    public class UploadController : Controller
    {
        //
        // GET: /Upload/

        public PartialViewResult Index()
        {

            return PartialView();

        }

        //
        // POST: /Upload/
        
        [HttpPost]
        public JsonResult Index(int? id)
        {
            try
            {
                var filename = string.Empty;
                foreach (string upload in Request.Files)
                {
                    //TODO: apgar esse comentario
                    //string path = AppDomain.CurrentDomain.BaseDirectory + "uploads\\";
                    //TODO: Lembrar de retirar o nome "uploads"
                    string path = @"C:\Users\Joilson\Desktop\FAMA\trunk\inputs\uploads";
                    //Directory.CreateDirectory(path);
                    filename = Path.GetFileName(Request.Files[upload].FileName);
                    Request.Files[upload].SaveAs(Path.Combine(path, filename));
                }

                //TODO: o segundo parametro "MaisProvavel" deve ser descoberto dinamicamente. Pode ser qq outro metodo.
                return Json(new { Success = true, Filename = filename }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new {Success = false, Message = "Ocorreu um erro ao fazer upload do arquivo."}, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
